package per.turtle.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import per.turtle.common.core.web.controller.BaseController;
import per.turtle.common.core.web.entity.CommonResult;
import per.turtle.common.core.web.page.TableDataInfo;
import per.turtle.common.excel.annotation.ResponseExcel;
import per.turtle.common.log.annotation.Log;
import per.turtle.common.log.enums.BusinessType;
import per.turtle.common.security.annotation.PreAuthorize;
import per.turtle.system.entity.SysPost;
import per.turtle.system.service.ISysPostService;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author turtle
 */
@AllArgsConstructor
@Api(value = "post", tags = {"post_岗位管理模块"})
@RequestMapping("post")
@RestController
public class SysPostController extends BaseController {

    private final ISysPostService postService;

    @ApiOperation(value = "新增岗位", notes = "新增岗位")
    @PreAuthorize(hasPermission = "system:post:add")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public CommonResult<Void> add(@Validated @RequestBody SysPost post) {
        if (!postService.checkPostNameUnique(post)) {
            return CommonResult.error("新增岗位【" + post.getPostName() + "】失败，岗位名称已存在");
        } else if (!postService.checkPostCodeUnique(post)) {
            return CommonResult.error("新增岗位【" + post.getPostName() + "】失败，岗位编码已存在");
        }
        return result(postService.insertPost(post));
    }

    @ApiOperation(value = "修改岗位", notes = "修改岗位")
    @PreAuthorize(hasPermission = "system:post:edit")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping("edit")
    public CommonResult<Void> edit(@Validated @RequestBody SysPost post) {
        if (!postService.checkPostNameUnique(post)) {
            return CommonResult.error("修改岗位【" + post.getPostName() + "】失败，岗位名称已存在");
        } else if (!postService.checkPostCodeUnique(post)) {
            return CommonResult.error("修改岗位【" + post.getPostName() + "】失败，岗位编码已存在");
        }
        return result(postService.updatePost(post));
    }

    @ApiOperation(value = "导出岗位", notes = "导出岗位")
    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @PreAuthorize(hasPermission = "system:post:export")
    @PostMapping("export")
    @ResponseExcel("岗位管理")
    public List<SysPost> export(HttpServletResponse response, @RequestBody SysPost post) {
        return postService.selectPostList(post);
    }

    @ApiOperation(value = "根据岗位编号获取详细信息", notes = "根据岗位编号获取详细信息")
    @PreAuthorize(hasPermission = "system:post:query")
    @GetMapping("{postId}")
    public CommonResult<SysPost> getInfo(@PathVariable Long postId) {
        return CommonResult.success(postService.selectPostById(postId));
    }

    @ApiOperation(value = "获取岗位列表或分页", notes = "获取岗位列表或分页（区别于是否传入分页参数）")
    @PostMapping("list")
    @PreAuthorize(hasPermission = "system:post:list")
    public TableDataInfo<SysPost> list(@RequestBody SysPost post) {
        List<SysPost> list = startPage(s -> postService.selectPostList(post));
        return getDataTable(list);
    }

    @ApiOperation(value = "获取岗位选择框列表", notes = "获取岗位选择框列表")
    @GetMapping("optionSelect")
    public CommonResult<List<SysPost>> optionSelect() {
        List<SysPost> posts = postService.selectPostAll();
        return CommonResult.success(posts);
    }

    @ApiOperation(value = "删除岗位", notes = "删除岗位")
    @PreAuthorize(hasPermission = "system:post:remove")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("{postIds}")
    public CommonResult<Void> remove(@PathVariable List<Long> postIds) {
        return result(postService.deletePostByIds(postIds));
    }

}
